package com.example.config;
import com.baidu.aip.imageclassify.AipImageClassify;
import com.baidu.aip.util.Base64Util;
import com.example.config.baiduAi.FileUtil;
import com.example.config.baiduAi.GsonUtils;
import com.example.config.baiduAi.HttpUtil;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("ai")
class Sample {
    public static final String API_KEY = "eAyxQGsGG7rNlWh7BuLNh8bo";
    public static final String SECRET_KEY = "G81y2p1qmgG00j4GYdGWlH0ZKUkxuyaK";

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */

    @PostMapping("dog")
    public static String advancedGeneral(@RequestBody MultipartFile file) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
        try {
            // 本地文件路径
//            byte[] imgData = FileUtil.readFileByBytes(file.toString());
            String imgStr = Base64Util.encode(file.getBytes());
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.25627fd88ca1bafaed90a0bad3f747af.2592000.1671756561.282335-28585012";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("plant")
    public static String plant(@RequestBody MultipartFile file) {
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant";
            try {
                AipImageClassify client = new AipImageClassify("28585012","eAyxQGsGG7rNlWh7BuLNh8bo","G81y2p1qmgG00j4GYdGWlH0ZKUkxuyaK");
            // 本地文件路径
            String imgStr = Base64Util.encode(file.getBytes());
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            /*设置参数     图片*/
            String param = "image=" + imgParam+"&baike_num=5";
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.25627fd88ca1bafaed90a0bad3f747af.2592000.1671756561.282335-28585012";
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return null;
    }



    public static String imgCombination() {
        // 请求url
        String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("imgUrl", "http://mayiserviceshop.oss-cn-nanjing.aliyuncs.com/images/722aa15d-ec74-4f34-aa30-f33592a1301bv2-3866b4bb491c090135930d083739a97d_720w.jpg");
            List<Object> scenes = new ArrayList<>();
            scenes.add("animal");
            scenes.add("plant");
            scenes.add("ingredient");
            scenes.add("dishs");
            scenes.add("red_wine");
            scenes.add("currency");
            scenes.add("landmark");
            map.put("scenes", scenes);
            map.put("baike_num", "5");

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.25627fd88ca1bafaed90a0bad3f747af.2592000.1671756561.282335-28585012";

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
    }

}